﻿Imports System.ComponentModel

Namespace Forms
    Public Class MDIParentFormBase
        Inherits Form

        Private WithEvents tabControl1 As TabControl
        Private components As Container = Nothing
        'Dim childCount As Integer = 1

        Public Sub New()
            InitializeComponent()
        End Sub

        Sub ShowChild(childForm As MDIChildFormBase)
            If Me.MdiChildren.Contains(childForm) Then
                childForm.Select()
                tabControl1.SelectedTab = childForm.TabPag
                Exit Sub
            End If

            childForm.MdiParent = Me
            childForm.TabCtrl = tabControl1
            Dim tp As New TabPage
            tp.Parent = tabControl1
            tp.Text = childForm.Text
            tp.Show()
            childForm.TabPag = tp
            childForm.Show()
            'childCount += 1
            tabControl1.SelectedTab = tp
        End Sub

        Protected Overrides Sub Dispose(disposing As Boolean)
            If disposing Then
                If components IsNot Nothing Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub

        Private Sub InitializeComponent()
            tabControl1 = New TabControl()
            SuspendLayout()

            tabControl1.Dock = System.Windows.Forms.DockStyle.Top
            tabControl1.Font = New System.Drawing.Font("Arial", 8.25F, (CType((System.Drawing.FontStyle.Bold Or System.Drawing.FontStyle.Italic), FontStyle)), System.Drawing.GraphicsUnit.Point, (CByte(0)))
            tabControl1.Location = New System.Drawing.Point(0, 0)
            tabControl1.Height = 24
            tabControl1.Name = "tabControl1"
            tabControl1.SelectedIndex = 0
            tabControl1.SizeMode = System.Windows.Forms.TabSizeMode.FillToRight
            tabControl1.TabIndex = 1
            tabControl1.Visible = False

            Controls.Add(tabControl1)
            IsMdiContainer = True
            ResumeLayout(False)
        End Sub

        Private Sub tabControl1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles tabControl1.SelectedIndexChanged
            For Each c As MDIChildFormBase In MdiChildren
                If c.TabPag.Equals(tabControl1.SelectedTab) Then
                    c.Select()
                End If
            Next
        End Sub
    End Class
End Namespace